#include<bits/stdc++.h>
using namespace std;
int n,m,base,res=1,T;
string s;
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    srand(time(0));
    cin>>n>>m;
    /*for(int i=1;i<=n*2;i++){
        int x;
        cin>>x;
    }*/
    while(res<n){
        if(res>=n){
            break;
        }
        res*=2;
        base++;
    }
    for(int i=1;i<=base;i++){
        cin>>s;
    }
    cin>>T;
    for(int i=1;i<=T;i++){
        int a,b,c,d;
        cin>>a>>b>>c>>d;
        cout<<(rand()%(n*n*m))<<endl;
    }
    return 0;
}
//`~1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./'!@#$%^&*()_+QWERTYUIOP{|ASDFGHJKL:"ZXCVBNM<>?"}
